<?php

namespace App\V3\V3_2_6\Models;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class SupplierCustomerModel extends Model
{


    protected $table = 'supplier_customer';

    protected $dateFormat = 'U';//这个时间字段被为被格式化为UNIX 时间戳的形式存储

    /**
     * toArray 时间转换问题
     * change 在 2021/10/14 14:58 创建
     * @param DateTimeInterface $date
     * @return string
     */
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    //检查供应商或客户信息
    static public function checkInfo($data)
    {
        return self::where([
            'compony_name' => $data['compony_name'],
            'name' => $data['name'],
            'phone' => $data['phone'],
            'type' => $data['type'],
            'address' => $data['address'],
            'user_id' => $data['user_id'],
        ])->first();
    }

    //列表
    static public function index($keyword, $data)
    {
        $query = self::select(
            'a.id',
            'a.compony_name',
            'a.name',
            'a.phone','a.address','a.created_at','b.username',
            'home_phone',
            'a.remark',
            'type'
        )
            ->from('supplier_customer as a')
            ->leftJoin('admin as b', 'a.user_id', '=', 'b.id')
            /* ->where('type', $data['type']) */
            ->where('a.delete_flag', 0)
            ->where(function($query) use ($keyword) {
                $query->orWhere('name', 'LIKE', '%'.$keyword.'%');
                $query->orWhere('compony_name', 'LIKE', '%'.$keyword.'%');
                $query->orWhere('phone', 'LIKE', '%'.$keyword.'%');
                $query->orWhere('address', 'LIKE', '%'.$keyword.'%');
                $query->orWhere('home_phone', 'LIKE', '%'.$keyword.'%');
            });

        if($data['is_share_supplier']) {//开启共享供应商
            $query->where('main_id', $data['main_id']);
            $query->where('type', $data['type']);
        }else{//未开启共享供应商
            $query->where('main_id', $data['main_id']);
            $query->where('user_id', $data['user_id']);
            $query->where('type', $data['type']);
        }

        return $query->orderBy('a.id','desc')
            ->paginate($data['pageSize'],['*'], '',$data['currentPage'])->toArray();
    }

    //列表
    static public function customerIndex($keyword, $data)
    {
        $query = self::select(
            'a.id',
            'a.compony_name',
            'a.name',
            'a.phone','a.address','a.created_at','b.username',
            'home_phone',
            'a.remark',
            'type'
            )
            ->from('supplier_customer as a')
            ->leftJoin('admin as b', 'a.user_id', '=', 'b.id')
            ->where('type', $data['type'])
        ->where('a.delete_flag', 0)
        ->where(function($query) use ($keyword) {
            $query->orWhere('name', 'LIKE', '%'.$keyword.'%');
            $query->orWhere('compony_name', 'LIKE', '%'.$keyword.'%');
            $query->orWhere('phone', 'LIKE', '%'.$keyword.'%');
            $query->orWhere('address', 'LIKE', '%'.$keyword.'%');
            $query->orWhere('home_phone', 'LIKE', '%'.$keyword.'%');
        });

            if($data['main_id'] == $data['user_id'] || $data['is_share_customer']) {
             $query->where('main_id', $data['main_id']);
             }
             else {
             $query->where('user_id', $data['user_id']);
             }

            return $query->orderBy('a.id','desc')
            ->paginate($data['pageSize'],['*'], '',$data['currentPage'])->toArray();
    }
    //今日新增客户
    static public function getTodayAddCustomer($condition, $startTime, $endTime){
    return self::where($condition)
            ->where('type',1)
            ->where('delete_flag',0)
            ->whereBetween('created_at', [$startTime, $endTime])
            ->count();
    }
}
